<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_setup_lokasi_tempat extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_setup_lokasi_tempat_model', 'LokasiTempat');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Lokasi Tempat";

		$query1 = $this->db->query("SELECT id_lokasi FROM (SELECT * FROM pro_lokasi_tempat ORDER BY id_lokasi DESC) WHERE rownum <=1");
				 				if ($query1->row_array()) {
				 					$getquery1 = $query1->row_array();
				 					$rowcount1 = $getquery1['id_lokasi'];
				 					$autonum1 = $rowcount1 + 1;
				 				} else {
								$autonum1 = 1;
							}
		$data['id_lokasi_auto'] = $autonum1;

		viewByTemplate('pro_setup_lokasi_tempat/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->LokasiTempat->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("lokasi_tempat") == '') {
			$error->Add("lokasi_tempat", "lokasi_tempat", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM pro_lokasi_tempat WHERE lokasi_tempat= '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("lokasi_tempat"))))."'")) > 0) {
			$error->Add("lokasi_tempat", "Lokasi Tempat", $tpl_duplicate);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
					$data	=  array(
					'id_lokasi' => $this->input->post('id_lokasi'),
					'lokasi_tempat' => $this->input->post('lokasi_tempat'),
					);

				if (post("mode") == "create")
				{
					$this->LokasiTempat->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->LokasiTempat->Update($data, array('id_lokasi'	=> $data['id_lokasi']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$id_lokasi	= get('id_lokasi');
		
		if ($id_lokasi != "")
		{
			// Delete Single
			$arr_rule	= array('id_lokasi' => $id_lokasi);
			$ls_result = $this->LokasiTempat->DeleteSingle($arr_rule);

		}
		else
		{
		}
		
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_lokasi' => $row['id_lokasi']);
				$ls_result .= $this->LokasiTempat->DeleteSingle($arr_rule);
			}
		echo $ls_result;
	}
}
?>